﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Collections.ObjectModel;

namespace CitrixPVSWrapper
{
   public static partial class PvsHelpers
    {
        /// <summary>
        /// Returns a string of the value requested. Property name is compared in lowercase, so input does not matter. 
        /// </summary>
        /// <param name="PropertyName"></param>
        /// <param name="Record"></param>
        /// <param name="Debug"></param>
        /// <returns></returns>
        public static string Get_StringValueFromRecord(string PropertyName, string[]Record, bool Debug = false)
        {
            if (Debug) { Console.WriteLine("Requested property: " + PropertyName + " from record"); }
            string Output = "";

            if (Record.Where(n => n.ToLower().StartsWith(PropertyName.ToLower() + ": ")).Count() > 0)
            {
                Output = Record.Where(n => n.ToLower().StartsWith(PropertyName.ToLower() + ": ")).First().Substring(PropertyName.Length+2).Trim();
                if (Debug) { Console.WriteLine("Property found. Value: " + Output); }
            }
            else
            {
                if (Debug) { Console.WriteLine("Property name " + PropertyName + "not found in record"); }
            }

            return Output;
        }

        public static List<string> Get_StringArrayValueFromRecord(string PropertyName, string[] Record, char Delimeter = ',', bool Debug = false)
        {
            string[] Output = null;
            String StringValue = Get_StringValueFromRecord(PropertyName, Record, Debug);
            try
            {
                Output = StringValue.Split(Delimeter);
                if (Debug) { Console.WriteLine("Splitting string to array succeeded. Array items: " + String.Join(", ",Output)); }
            }
            catch (Exception e)
            {
                if (Debug) { Console.WriteLine("Splitting string failed: " + e.Message); }
            }
            return Output.ToList();
        }

        public static UInt32 Get_UInt32ValueFromRecord(string PropertyName, string[] Record, bool Debug = false)
        {
            UInt32 Output = new UInt32();
            String StringValue = Get_StringValueFromRecord(PropertyName, Record, Debug);
            try
            {
                Output = Convert.ToUInt32(StringValue);
                if (Debug) { Console.WriteLine("Conversion to UInt32 Success. Output: " + Output); }
            }
            catch (FormatException)
            {
                if (Debug) { Console.WriteLine("Conversion to UInt32 Failed. Source data is not in a valid format. Source Data: " + StringValue); }
            }
            return Output;
        }

        public static UInt64 Get_UInt64ValueFromRecord(string PropertyName, string[] Record, bool Debug = false)
        {
            UInt64 Output = new UInt64();
            String StringValue = Get_StringValueFromRecord(PropertyName, Record, Debug);
            try
            {
                Output = Convert.ToUInt64(StringValue);
                if (Debug) { Console.WriteLine("Conversion to UInt64 Success. Output: " + Output); }
            }
            catch (FormatException)
            {
                if (Debug) { Console.WriteLine("Conversion to UInt64 Failed. Source data is not in a valid format. Source Data: " + StringValue); }
            }
            return Output;
        }

        public static DateTime Get_DateTimeValueFromRecord(string PropertyName, string[] Record, bool Debug = false)
        {
            DateTime Output = new DateTime();
            String StringValue = Get_StringValueFromRecord(PropertyName, Record, Debug);
            try
            {
                Output = Convert.ToDateTime(StringValue);
                if (Debug) { Console.WriteLine("Conversion to DateTime Success. Output: " + Output); }
            }
            catch (FormatException)
            {
                if (Debug) { Console.WriteLine("Conversion to DateTime Failed. Source data is not in a valid format. Source Data: " + StringValue); }
            }
            return Output;
        }

        public static Boolean Get_BooleanValueFromRecord(string PropertyName, string[] Record, bool Debug = false)
        {
            return (Get_StringValueFromRecord(PropertyName, Record, Debug) == "1"); ;
        }

    }
}

